<?php
/*
 * @desc:	网上商城的接口
 * @author:	cpj
 * @date:	2012-1-4
 * @copyright:	dld.com
 * 
 * @params:	方法返回的数据格式均为:	data = array(
 * 												'page' => array('pageSize' => xx,'pageCount' => xx),
 * 												'data' = array()
 * 												)
 * 相应的商城API代码在:商城目录/tools/
 * 待处理:	加解密
 */
class MallApi 
{
	private $_mallHost = '';
	private $_sKey = '4b0b06e2966ec27815f83ebdaa195aa9';
	
	
	public function __construct()
	{
		$this->connect();
	}
	
	private function connect()
	{
		$mallHost = Yii::app()->params['mallUrl'];
		$res = get_headers($mallHost);
		if($res)
		{
			$this->_mallHost = $mallHost;
			return TRUE;
		}
		else
		{
			Throw new Exception('Connecting to Mall Fail!',404);
		}
	}
	
	/**
	 * 获取该用户买家提醒
	 */
	public function getBuyerOrderInfo($uid)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=buyOrder&id=' . $uid;
    	$data = $this->get($requestUrl);
    	return $data;
	}
	
	/**
	 * 获取该用户卖家提醒
	 */
	public function getSaleOrderInfo($uid)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=saleOrder&id=' . $uid;
    	$data = $this->get($requestUrl);
    	return $data;
	}
	
	
	/**	
	 * 获取该用户在商城的收藏商品
	 * @params:	$uid:用户ID,$page:页数,$ps:page size
	 */
	public function getGoodsFavorite($uid,$page = 1,$ps = 5)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=goodsFavorite&id=' . $uid . '&page=' . $page . '&ps=' . $ps;
    	$data = $this->get($requestUrl);
    	return $data;
	}
	
	/**
	 * 获取该用户的收藏网店
	 */
	public function getMallShopFavorite($uid,$page = 1,$ps = 5)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=shopFavorite&id=' . $uid . '&page=' . $page . '&ps=' . $ps;
		$data = $this->get($requestUrl);
		return $data;
	}
	
	/**
	 * 获取我的网店收到的留言
	 */
	public function getMyShopMessage($shop_id,$page = 1,$ps = 5)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=getMessage&shop_id=' . $shop_id . '&page=' . $page . '&ps=' . $ps;
		$data = $this->get($requestUrl);
		return $data;
	}
	
	/**
	 * 获取我作为卖家，收到的(买家)评价
	 */
	public function getBuyerComment($user_id,$page = 1,$ps = 5)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=getBuyerComment&user_id=' . $user_id . '&page=' . $page . '&ps=' . $ps;
		$data = $this->get($requestUrl);
		return $data;
	}
	
	/**
	 * 获取我作为买家,收到的(卖家)评价
	 */
	public function getSaleComment($user_id,$page = 1,$ps = 5)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=getSaleComment&user_id=' . $user_id . '&page=' . $page . '&ps=' . $ps;
		$data = $this->get($requestUrl);
		return $data;
	}
	
	/**
	 * 获取商城的登录状态
	 */
	public function checkLoginStatus()
	{
		
	}
	
	/**
	 * 注销商城
	 */
	public function logout()
	{
		$requestUrl = $this->_mallHost . '/do.php?act=logout';
		return $requestUrl;
//		
//		$data = $this->get($requestUrl);
//		return $data;
	}
	
	/**
	 * 更新老用户在商城的ID
	 */
	public function upgradeId($newId,$oldId)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=upgrade&uid1='.$oldId.'&uid2='.$newId;
		$data = $this->get($requestUrl);
		return $data;
	}
	
	/**
	 *	取得该用户的消费列表
	 */
	public function getUserOrderList($uid,$page = 1,$pageSize =10)
	{
		$requestUrl = $this->_mallHost . '/tools/act.php?act=getOrderList&user_id=' . $uid . '&page=' . $page . '&ln=' . $pageSize;
		$data = $this->get($requestUrl);
		return $data;		
	}
	
	/**
	 * 取得url的数据
	 */
	private function get($url)
	{
		$data = file_get_contents($url. '&sKey=' . $this->_sKey);
		return json_decode($data);
	}
	
	
}